<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>字母歌节奏游戏</title>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Arial Rounded MT Bold', 'Helvetica Rounded', Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
            min-height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #333;
            padding: 20px;
            overflow-x: hidden;
        }
        /* 游戏容器 */
        #game-container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.95);
            padding: 2rem;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.15);
            width: 100%;
            max-width: 600px;
            margin-top: 20px;
            position: relative;
            overflow: hidden;
        }
        /* 标题 */
        h1 {
            color: #764ba2;
            margin-bottom: 1.5rem;
            font-size: 2.2rem;
            text-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        /* 控制区域样式 */
        .controls-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 1.5rem;
            width: 100%;
        }
        .buttons-container {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 1rem;
            width: 100%;
        }
        /* 游戏信息区域 */
        #game-info {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            background-color: rgba(118, 75, 162, 0.1);
            padding: 1rem;
            border-radius: 15px;
            margin-bottom: 1.5rem;
            text-align: center;
        }
        /* 按钮样式 */
        button {
            padding: 1rem 2rem;
            font-size: 1.2rem;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            flex: 1;
            max-width: 200px;
        }
        #start-button {
            background-color: #4CAF50;
            color: white;
        }
        #start-button:hover {
            background-color: #45a049;
            transform: translateY(-3px);
            box-shadow: 0 6px 10px rgba(0,0,0,0.15);
        }
        #demo-button {
            background-color: #2196F3;
            color: white;
        }
        #demo-button:hover {
            background-color: #0b7dda;
            transform: translateY(-3px);
            box-shadow: 0 6px 10px rgba(0,0,0,0.15);
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        /* 字母显示 */
        #letter-display {
            font-size: 3.5rem;
            margin: 2rem 0;
            height: 80px;
            transition: all 0.3s ease;
            font-weight: bold;
            color: #333;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100px;
        }
        #letter-display.highlight {
            transform: scale(1.2);
            color: #4CAF50;
            text-shadow: 0 0 15px rgba(76, 175, 80, 0.5);
        }
        /* 状态和关卡显示 */
        #status-display, #level-display {
            font-size: 1rem;
            margin-bottom: 0.5rem;
            font-weight: bold;
            width: 100%;
        }
        #status-display {
            color: #e91e63;
        }
        #level-display {
            color: #ff9800;
        }
        /* 难度选择 */
        #difficulty-selection {
            margin-bottom: 1.5rem;
            font-size: 1.1rem;
        }
        #difficulty {
            padding: 0.5rem;
            border-radius: 5px;
            border: 1px solid #ddd;
            margin-left: 0.5rem;
            font-size: 1rem;
        }
        /* 音频警告 */
        #audio-warning {
            color: #ff9800;
            margin-top: 1rem;
            font-size: 0.9rem;
            line-height: 1.5;
        }
        /* 图片展示 */
        #sheet-music-container {
            margin: 1.5rem 0;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        #sheet-music {
            width: 100%;
            max-height: 300px;
            object-fit: cover;
            transition: transform 0.3s ease;
        }
        #sheet-music:hover {
            transform: scale(1.02);
        }
        /* 虚拟键盘样式 */
        #virtual-keyboard {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 1.5rem;
        }
        .key {
            width: 70px;
            height: 70px;
            border-radius: 15px;
            font-size: 1.5rem;
            background-color: #764ba2;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0;
            margin: 0.3rem;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            transition: all 0.1s ease;
        }
        .key:active {
            transform: scale(0.95);
            background-color: #667eea;
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }
        .key:hover {
            background-color: #667eea;
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.25);
        }
        /* 响应式设计 */
        @media (max-width: 600px) {
            #game-container {
                padding: 1.5rem;
                margin-top: 10px;
            }
            h1 {
                font-size: 1.8rem;
            }
            #letter-display {
                font-size: 2.5rem;
                min-height: 80px;
            }
            button {
                padding: 0.8rem 1.5rem;
                font-size: 1rem;
            }
            #sheet-music-container {
                margin: 1rem 0;
            }
        }
        @media (max-width: 400px) {
            h1 {
                font-size: 1.5rem;
            }
            #letter-display {
                font-size: 2rem;
            }
            button {
                padding: 0.7rem 1.2rem;
                font-size: 0.9rem;
                margin: 0.3rem;
            }
        }
    </style>
</head>
<body>
    <div id="game-container">
        <h1>字母歌节奏游戏</h1>
        <div class="controls-container">
            <div id="difficulty-selection">
                <label>难度选择:</label>
                <select id="difficulty">
                    <option value="easy">简单 (2秒)</option>
                    <option value="medium">中等 (1秒)</option>
                    <option value="hard">困难 (500毫秒)</option>
                </select>
            </div>
            <div class="buttons-container">
                <button id="demo-button">演示播放</button>
                <button id="start-button">开始游戏</button>
            </div>
        </div>
        <div id="game-info">
            <div id="level-display">关卡: 1</div>
            <div id="status-display">点击开始按钮开始游戏</div>
        </div>
        <div id="letter-display">A</div>
        <div id="sheet-music-container">
            <img id="sheet-music" src="字母歌简谱.jpg" alt="字母歌简谱">
        </div>
        <div id="audio-warning">注意：请确保您的设备音量已开启；根据字母歌的简谱来按对应数字进行游戏，按错或超时都会导致结束游戏；关卡越高速度越快；</div>
        
        <!-- 虚拟键盘 -->
        <div id="virtual-keyboard">
            <button class="key">1</button>
            <button class="key">2</button>
            <button class="key">3</button>
            <button class="key">4</button>
            <button class="key">5</button>
            <button class="key">6</button>
            <button class="key">7</button>
        </div>

    </div>

    <script>
        // 创建音频上下文用于生成字母歌旋律
        let audioContext;
        // 音符频率 (扩展C大调音阶)
        const notes = {
            'C4': 261.63,
            'D4': 293.66,
            'E4': 329.63,
            'F4': 349.23,
            'G4': 392.00,
            'A4': 440.00,
            'B4': 493.88,
            'C5': 523.25,
            'D5': 587.33,
            'E5': 659.25,
            'F5': 698.46,
            'G5': 783.99,
            'A5': 880.00,
            'B5': 987.77,
            'C6': 1046.50,
            'D6': 1174.66,
            'E6': 1318.51,
            'F6': 1396.91
        };
       
        // 简谱数字到音符的映射
        const numberToNote = {
            '1': 'C4',  // 简谱1对应C4
            '2': 'D4',  // 简谱2对应D4
            '3': 'E4',  // 简谱3对应E4
            '4': 'F4',  // 简谱4对应F4
            '5': 'G4',  // 简谱5对应G4
            '6': 'A4',  // 简谱6对应A4
            '7': 'B4'   // 简谱7对应B4
        };

        // 字母歌歌词与简谱数字的映射 (duration: 四分之一拍=500ms)
        const lyricsWithNumbers = [
            { lyric: 'Aa', number: '1', duration: 500 },  // 四分之一拍
            { lyric: 'Bb', number: '1', duration: 500 },  // 四分之一拍
            { lyric: 'Cc', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Dd', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Ee', number: '6', duration: 500 },  // 四分之一拍
            { lyric: 'Ff', number: '6', duration: 500 },  // 四分之一拍
            { lyric: 'Gg', number: '5', duration: 1000 }, // 四分之二拍
            { lyric: 'Hh', number: '4', duration: 500 },  // 四分之一拍
            { lyric: 'Ii', number: '4', duration: 500 },  // 四分之一拍
            { lyric: 'Jj', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Kk', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Ll', number: '2', duration: 500 },  // 四分之一拍
            { lyric: 'Mm', number: '2', duration: 500 },  // 四分之一拍
            { lyric: 'Nn', number: '1', duration: 1000 }, // 四分之二拍
            { lyric: 'Oo', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Pp', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Qq', number: '4', duration: 1000 },  // 四分之一拍
            { lyric: 'Rr', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Ss', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Tt', number: '2', duration: 1000 }, // 四分之二拍
            { lyric: 'Uu', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Vv', number: '5', duration: 500 },  // 四分之一拍
            { lyric: 'Ww', number: '4', duration: 1000 },  // 四分之一拍
            { lyric: 'Xx', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Yy', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'Zz', number: '2', duration: 1000 }, // 四分之二拍
            { lyric: 'Xx', number: '1', duration: 500 },  // 四分之一拍
            { lyric: 'Yy', number: '1', duration: 500 },  // 四分之一拍
            { lyric: 'Zz', number: '5', duration: 1000 },  // 四分之一拍
            { lyric: 'Now', number: '6', duration: 500 },  // 四分之一拍
            { lyric: 'you', number: '6', duration: 500 },  // 四分之一拍
            { lyric: 'see', number: '5', duration: 1000 }, // 四分之二拍
            { lyric: 'I', number: '4', duration: 500 },   // 四分之一拍
            { lyric: 'con', number: '4', duration: 500 },  // 四分之一拍
            { lyric: 'say', number: '3', duration: 500 },  // 四分之一拍
            { lyric: 'my', number: '3', duration: 500 },   // 四分之一拍
            { lyric: 'Aa', number: '2', duration: 500 },   // 四分之一拍
            { lyric: 'Bb', number: '2', duration: 500 },   // 四分之一拍
            { lyric: 'Cc', number: '1', duration: 1000 }  // 四分之三拍
        ];

        // 游戏变量
        let isGameRunning = false;
        let isDemoPlaying = false;
        let score = 0;
        let currentLyricIndex = 0;
        let currentLevel = 1;
        let gameTimer;
        let timePerLetter;
        
        // DOM元素
        const startButton = document.getElementById('start-button');
        const demoButton = document.getElementById('demo-button');
        const difficultySelect = document.getElementById('difficulty');
        const letterDisplay = document.getElementById('letter-display');
        const statusDisplay = document.getElementById('status-display');
        const levelDisplay = document.getElementById('level-display');
        
        // 事件监听器
        startButton.addEventListener('click', startGame);
        demoButton.addEventListener('click', startDemo);
        document.addEventListener('keydown', handleKeyPress);
        
        // 虚拟键盘事件监听
        const keys = document.querySelectorAll('.key');
        keys.forEach(key => {
            key.addEventListener('click', () => {
                const event = new KeyboardEvent('keydown', {
                    key: key.textContent
                });
                document.dispatchEvent(event);
            });
        });
        
        // 初始化音频上下文
        function initAudioContext() {
            if (!audioContext) {
                try {
                    audioContext = new (window.AudioContext || window.webkitAudioContext)();
                } catch (e) {
                    console.error('Web Audio API is not supported in this browser');
                    statusDisplay.textContent = '错误：您的浏览器不支持Web Audio API';
                }
            }
        }
        
        // 创建振荡器播放音符
        function playNote(noteName, duration = 500) {
            if (!audioContext) {
                initAudioContext();
                if (!audioContext) return;
            }
            
            // 处理休止符
            if (!noteName || noteName === '') {
                return;
            }
            
            const frequency = notes[noteName];
            if (!frequency) return;
            
            const oscillator = audioContext.createOscillator();
            const gainNode = audioContext.createGain();
            
            oscillator.type = 'sine';
            oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
            
            gainNode.gain.setValueAtTime(0, audioContext.currentTime);
            gainNode.gain.linearRampToValueAtTime(0.3, audioContext.currentTime + 0.01);
            gainNode.gain.linearRampToValueAtTime(0, audioContext.currentTime + duration/1000 - 0.05);
            
            oscillator.connect(gainNode);
            gainNode.connect(audioContext.destination);
            
            oscillator.start();
            oscillator.stop(audioContext.currentTime + duration/1000);
        }
        
        // 开始游戏
        function startGame() {
            if (isGameRunning || isDemoPlaying) return;
            
            isGameRunning = true;
            currentLevel = 1;
            currentLyricIndex = 0;
            score = 0;
            
            // 根据难度设置初始时间
            const difficulty = difficultySelect.value;
            if (difficulty === 'easy') {
                timePerLetter = 2000; // 简单: 2秒
            } else if (difficulty === 'medium') {
                timePerLetter = 1000; // 中等: 1秒
            } else {
                timePerLetter = 500; // 困难: 500毫秒
            }
            
            // 初始化音频上下文
            initAudioContext();
            
            updateLevelDisplay();
            statusDisplay.textContent = '游戏进行中...';
            startButton.disabled = true;
            demoButton.disabled = true;
            
            // 显示第一句歌词
            showNextLyric();
        }
        
        // 开始演示
        function startDemo() {
            if (isGameRunning || isDemoPlaying) return;
            
            isDemoPlaying = true;
            currentLyricIndex = 0;
            
            // 根据难度设置演示速度
            const difficulty = difficultySelect.value;
            if (difficulty === 'easy') {
                timePerLetter = 2000; // 简单: 2秒
            } else if (difficulty === 'medium') {
                timePerLetter = 1000; // 中等: 1秒
            } else {
                timePerLetter = 500; // 困难: 500毫秒
            }
            
            // 初始化音频上下文
            initAudioContext();
            
            statusDisplay.textContent = '演示播放中...';
            startButton.disabled = true;
            demoButton.disabled = true;
            
            // 开始演示播放
            playDemoNextNote();
        }
        
        // 播放演示的下一个音符
        function playDemoNextNote() { 
            if (currentLyricIndex >= lyricsWithNumbers.length) {
                isDemoPlaying = false;
                statusDisplay.textContent = '演示结束，请点击开始游戏';
                startButton.disabled = false;
                demoButton.disabled = false;
                return;
            }
            
            const currentItem = lyricsWithNumbers[currentLyricIndex];
            
            // 显示歌词和简谱数字并添加高亮效果
            letterDisplay.textContent = `${currentItem.lyric} (${currentItem.number})`;
            letterDisplay.classList.add('highlight');
            setTimeout(() => {
                letterDisplay.classList.remove('highlight');
            }, 200);
            
            // 获取对应的音符和时长并播放
            const currentNote = numberToNote[currentItem.number];
            playNote(currentNote, currentItem.duration);
            
            // 移动到下一句歌词，确保当前音符播放完成后再播放下一个
            currentLyricIndex++;            
            // 使用当前音符时长和难度时间中的较大值，避免声音重叠
            const nextDelay = Math.max(currentItem.duration, timePerLetter);
            setTimeout(playDemoNextNote, nextDelay);
        }
        
        // 显示下一句歌词
        function showNextLyric() {
            if (currentLyricIndex >= lyricsWithNumbers.length) {
                // 完成所有歌词，通关
                levelUp();
                return;
            }
            
            const currentItem = lyricsWithNumbers[currentLyricIndex];
            
            // 显示歌词和对应的简谱数字
            letterDisplay.textContent = `${currentItem.lyric} (${currentItem.number})`;
            letterDisplay.classList.add('highlight');
            setTimeout(() => {
                letterDisplay.classList.remove('highlight');
            }, 200);
            
            // 设置计时器，如果在规定时间内没有按对则游戏结束
            clearTimeout(gameTimer);
            gameTimer = setTimeout(gameOver, timePerLetter);
        }
        
        // 处理按键
        function handleKeyPress(event) {
            if (!isGameRunning) return;
            
            const pressedNumber = event.key;
            const currentItem = lyricsWithNumbers[currentLyricIndex];
            
            // 检查是否按了正确的简谱数字
            if (pressedNumber === currentItem.number) {
                // 按键正确
                clearTimeout(gameTimer);
                score++;
                
                // 获取对应的音符
                const currentNote = numberToNote[currentItem.number];
                
                // 播放音符
                playNote(currentNote);
                
                // 显示正确反馈
                statusDisplay.textContent = `正确！得分: ${score}`;
                setTimeout(() => {
                    if (isGameRunning) {
                        statusDisplay.textContent = '游戏进行中...';
                    }
                }, 300);
                
                // 移动到下一句歌词
                currentLyricIndex++;
                showNextLyric();
            } else {
                // 按键错误
                gameOver();
            }
        }
        
        // 升级
        function levelUp() {
            clearTimeout(gameTimer);
            isGameRunning = false;
            
            currentLevel++;
            
            // 根据难度调整每关减少的时间
            const difficulty = difficultySelect.value;
            let baseTime, decreasePerLevel;
            if (difficulty === 'easy') {
                baseTime = 2000;
                decreasePerLevel = 200; // 每关减少200ms
            } else if (difficulty === 'medium') {
                baseTime = 1000;
                decreasePerLevel = 100; // 每关减少100ms
            } else {
                baseTime = 500;
                decreasePerLevel = 50; // 每关减少50ms
            }
            
            timePerLetter = Math.max(300, baseTime - (currentLevel - 1) * decreasePerLevel);
            
            updateLevelDisplay();
            statusDisplay.textContent = `恭喜！进入第${currentLevel}关！时间限制: ${timePerLetter}ms`;
            startButton.disabled = false;
            demoButton.disabled = false;
        }
        
        // 游戏结束
        function gameOver() {
            clearTimeout(gameTimer);
            isGameRunning = false;
            
            statusDisplay.textContent = `游戏结束！得分: ${score}，达到了第${currentLevel}关`;
            startButton.disabled = false;
            demoButton.disabled = false;
        }
        
        // 更新关卡显示
        function updateLevelDisplay() {
            levelDisplay.textContent = `关卡: ${currentLevel}`;
        }
    </script>
</body>
</html>